/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rpt.bd.cenario;

import java.awt.Component;
import java.sql.ResultSet;
import java.text.ParseException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import rpt.lib.CCalendarioComboBox;

/**
 * A classe CRegistroBd manipula o registro com os campos do banco de dados.
 * Os registros das tabelas dos bancos de dados são colecionados através
 * de um vetor.
 * @author Roberto
 */

/**************************************************************************/
/***                                                                    ***/
/**************************************************************************/

public class CRegistroBd extends CRegistro {

    CCamposComplementoRegistroBd fCamposComplementoRegistroBd = null;
    boolean fCamposComplementoRegistroBdCarregados = false;

    CPersistenciaBd fPersistenciaBd = null;
    CEstruturaRegistroBd fEstruturaRegistroBd = null;
    CCampoRegistroBd fCampoFocado = null;

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    /**
     * Construtor básico da classe CRegistroBd. Esta classe recebe como parâmetro
     * o cenário do registro.
     * @param aCenarioBd (CCenarioBd) Contém o cenário do registro.
     */
    public CRegistroBd (CPersistenciaBd aPertinenciaBd) {
        super();
        fPersistenciaBd = aPertinenciaBd;
        fEstruturaRegistroBd = fPersistenciaBd.getEstruturaRegistroBd();
        fCamposComplementoRegistroBd = new CCamposComplementoRegistroBd(fPersistenciaBd);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CRegistroBd (CPersistenciaBd aPersistenciaBd, ResultSet aRs) {
        this(aPersistenciaBd);
        if (aRs == null)
            InserirCamposRegistroBd(this,fCamposComplementoRegistroBd);
        else
            adicionarCamposRegistroBd(aRs,this,fCamposComplementoRegistroBd);
        fCamposComplementoRegistroBdCarregados = true;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public Vector getCamposComplemento() {
        return fCamposComplementoRegistroBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CPersistenciaBd getPersistenciaBd() {
        return fPersistenciaBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void adicionarCamposRegistroBd(
        final ResultSet aRs, final Vector aCamposCabecalho, final Vector aCamposComplemento) {
        //
        CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = null;
        CCampoRegistroBd vCampoRegistroBd = null;
        CEstruturaRegistroBd vEstruturaRegistroBd = fPersistenciaBd.getEstruturaRegistroBd();
        int vNumCamposRegistroBd = vEstruturaRegistroBd.getNumCamposRegistroBd();
        for (int vContador = 0; vContador < vNumCamposRegistroBd; vContador++) {
            vEstruturaCampoRegistroBd = vEstruturaRegistroBd.getEstruturaCampoRegistroBd(vContador);
            if (vEstruturaCampoRegistroBd instanceof CEstruturaCampoRegistroBdLookup)
                vCampoRegistroBd =
                    new CCampoRegistroBdLookup(this,vEstruturaCampoRegistroBd,aRs);
            else
                vCampoRegistroBd =
                    new CCampoRegistroBd(this,vEstruturaCampoRegistroBd,aRs);
/*
            String[] vDet = fPersistenciaBd.getNomesCampoDetalhe();
            String vNomeCampo = vCampoRegistroBd.getEstruturaCampoRegistroBd().getNomeBd();
            if ((vDet != null) && (vDet.toLowerCase().equals(vNomeCampo.toLowerCase()))) {
                //vCampoRegistroBd.setValorCampoBd(new Integer(fPertinenciaBd.getValorIdMestre()).toString());
            }
*/
            if (vEstruturaCampoRegistroBd.getCampoGradeBd())
                aCamposCabecalho.add(vCampoRegistroBd);
            else
                aCamposComplemento.add(vCampoRegistroBd);
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void adicionarCamposRegistroBd(final ResultSet aRs) {
        CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = null;
        CEstruturaRegistroBd vEstruturaRegistroBd = fPersistenciaBd.getEstruturaRegistroBd();
        int vNumItensPertinenciaBd = vEstruturaRegistroBd.getNumCamposRegistroBd();
        for (int vContador = 0; vContador < vNumItensPertinenciaBd; vContador++) {
            vEstruturaCampoRegistroBd = vEstruturaRegistroBd.getEstruturaCampoRegistroBd(vContador);
            if (vEstruturaCampoRegistroBd.getCampoGradeBd())
                add(new CCampoRegistroBd(this,vEstruturaCampoRegistroBd,aRs));
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void transferirCamposRegistroBd(
        final CCampoRegistroBd aCampoRegistroBd,
        final String aNomeCampo,
        final String[] aNomesCampoMestre,
        final String[] aNomesCampoDetalhe) {
        //
        if (aNomesCampoDetalhe != null) {
            int[] vValoresIdMestre = fPersistenciaBd.getValoresIdMestre();
            for (int i = 0; i < aNomesCampoDetalhe.length; i++) {
                String vNomeCampoMestre = null;
                if (aNomesCampoMestre == null)
                    vNomeCampoMestre = aNomesCampoDetalhe[i];
                else
                    vNomeCampoMestre = aNomesCampoMestre[i];
                final String vNomeCampoDetalhe = aNomesCampoDetalhe[i];
                if (vNomeCampoDetalhe.toLowerCase().equals(aNomeCampo.toLowerCase())) {
                    final CPersistenciaBd pm = fPersistenciaBd.getPertinenciaBdMestre();
                    if (pm == null) {
                        aCampoRegistroBd.setValorCampoBd(
                            new Integer(vValoresIdMestre[i]).toString());
                    }
                    else {
                        //String[] vNomesCampoMestre = fPersistenciaBd.getNomesCampoMestre();
                        final CConjuntoDadosBd cd = pm.getConjuntoDadosBd();
                        final CRegistroBd r = cd.getRegistroBdAtual();
                        if (r != null) {
                           final CCampoRegistroBd cr = r.getCampoRegistroBd(vNomeCampoMestre); //r.getCampoRegistroBdChavePrimaria();
                           aCampoRegistroBd.setValorCampoBd(cr.getValorCampoBd());
                        }
//                      aCampoRegistroBd.setValorCampoBd(new Integer(cr.toString()).toString());
                    }
                }
            }
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void transferirCamposRegistroBdHeranca(
        final CCampoRegistroBd aCampoRegistroBd,
        final String aNomeCampo,
        final String[] aNomesCampoMestreHeranca,
        final String[] aNomesCampoDetalheHeranca) {
        //
        if (aNomesCampoDetalheHeranca != null) {
            //int[] vValoresIdMestre = fPersistenciaBd.getValoresIdMestre();
            final CPersistenciaBd pm = fPersistenciaBd.getPertinenciaBdMestre();
            if (pm != null) {
                for (int i = 0; i < aNomesCampoMestreHeranca.length; i++) {
                    String vNomeCampoMestreHeranca = null;
                    if (aNomesCampoMestreHeranca == null)
                        vNomeCampoMestreHeranca = aNomesCampoDetalheHeranca[i];
                    else
                        vNomeCampoMestreHeranca = aNomesCampoMestreHeranca[i];
                    // final String vNomeCampoDetalheHeranca = aNomesCampoDetalheHeranca[i];
                    if (vNomeCampoMestreHeranca.toLowerCase().equals(aNomeCampo.toLowerCase())) {
                        //String[] vNomesCampoMestreHeranca = fPersistenciaBd.getNomesCampoMestreHeranca();
                        //String vNomeCampoDetalheHeranca = vNomesCampoDetalheHeranca[i];
                        CConjuntoDadosBd cd = pm.getConjuntoDadosBd();
                        CRegistroBd r = cd.getRegistroBdAtual();
                        CCampoRegistroBd cr = r.getCampoRegistroBd(vNomeCampoMestreHeranca); //r.getCampoRegistroBdChavePrimaria();
                        aCampoRegistroBd.setValorCampoBd(cr.getValorCampoBd());
                    }
                }
            }
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void InserirCamposRegistroBd(
        Vector aCamposCabecalho, Vector aCamposComplemento) {
        //
        CEstruturaRegistroBd vEstruturaRegistroBd = fPersistenciaBd.getEstruturaRegistroBd();
        int vNumItensPertinenciaBd = vEstruturaRegistroBd.getNumCamposRegistroBd();
        for (int vContador = 0; vContador < vNumItensPertinenciaBd; vContador++) {
            CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = vEstruturaRegistroBd.getEstruturaCampoRegistroBd(vContador);
            CCampoRegistroBd vCampoRegistroBd =
                new CCampoRegistroBd(this,vEstruturaCampoRegistroBd,null);
            String vNomeCampo = vCampoRegistroBd.getEstruturaCampoRegistroBd().getNomeBd();
            String[] vNomesCampoMestre = fPersistenciaBd.getNomesCampoMestre();
            String[] vNomesCampoMestreHeranca = fPersistenciaBd.getNomesCampoMestreHeranca();
            String[] vNomesCampoDetalhe = fPersistenciaBd.getNomesCampoDetalhe();
            String[] vNomesCampoDetalheHeranca = fPersistenciaBd.getNomesCampoDetalheHeranca();
            transferirCamposRegistroBd(vCampoRegistroBd,vNomeCampo,vNomesCampoMestre,vNomesCampoDetalhe);
            transferirCamposRegistroBdHeranca(vCampoRegistroBd,vNomeCampo,vNomesCampoDetalheHeranca,vNomesCampoDetalheHeranca);
            if (vEstruturaCampoRegistroBd.getCampoGradeBd())
                aCamposCabecalho.add(vCampoRegistroBd);
            else
                aCamposComplemento.add(vCampoRegistroBd);
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void atribuirCabecalho(ResultSet aRs) {
        //int vNumCamposCabecalhoRegistroBd = fEstruturaRegistroBd.getNumCamposCabecalhoRegistroBd();
        for (int vContador = 0; vContador < fEstruturaRegistroBd.getNumCamposRegistroBd(); vContador++) {
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CCampoRegistroBd getCampoRegistroBd(int aIndice) {
        if ((aIndice < 0) || (aIndice >= size()))
            return null;
        else
            return (CCampoRegistroBd)get(aIndice);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CCampoRegistroBd getCampoRegistroBd(String aNomeCampo, Vector aVetorCamposRegistroBd) {
        CCampoRegistroBd vCampoRegistroBdLocalizado = null;
        int vNumCampos = aVetorCamposRegistroBd.size();
        CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = null;
        for (int vContador = 0; (vContador < vNumCampos) && (vCampoRegistroBdLocalizado == null); vContador++) {
            CCampoRegistroBd vCampoRegistroBd = (CCampoRegistroBd)aVetorCamposRegistroBd.get(vContador);
            if (vCampoRegistroBd != null) {
                vEstruturaCampoRegistroBd = vCampoRegistroBd.getEstruturaCampoRegistroBd();
                if (vEstruturaCampoRegistroBd.getNomeBd().toLowerCase().equals(aNomeCampo.toLowerCase())) {
                    vCampoRegistroBdLocalizado = vCampoRegistroBd;
                }
            }
        }
        return vCampoRegistroBdLocalizado;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CCampoRegistroBd getCampoRegistroBd(String aNomeCampo) {
        CCampoRegistroBd vCampoRegistroBd = getCampoRegistroBd(aNomeCampo,this);
        if (vCampoRegistroBd == null) {
            vCampoRegistroBd = getCampoRegistroBd(aNomeCampo,getCamposComplemento());
        }
        return vCampoRegistroBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public int getIndiceCampoRegistroBd(String aNomeCampo, Vector aVetorCamposRegistroBd) {
        CCampoRegistroBd vCampoRegistroBd = null;
        CCampoRegistroBd vCampoRegistroBdLocalizado = null;
        int vIndiceCampoRegistroBdLocalizado = -1;
        int vNumCampos = aVetorCamposRegistroBd.size();
        CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = null;
        for (int vContador = 0; (vContador < vNumCampos) && (vCampoRegistroBdLocalizado == null); vContador++) {
            vCampoRegistroBd = (CCampoRegistroBd)aVetorCamposRegistroBd.get(vContador);
            if (vCampoRegistroBd != null) {
                vEstruturaCampoRegistroBd = vCampoRegistroBd.getEstruturaCampoRegistroBd();
                if (vEstruturaCampoRegistroBd.getNomeBd().toLowerCase().equals(aNomeCampo.toLowerCase())) {
                    vCampoRegistroBdLocalizado = vCampoRegistroBd;
                    vIndiceCampoRegistroBdLocalizado = vContador;
                }
            }
        }
        return vIndiceCampoRegistroBdLocalizado;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public int getIndiceCampoRegistroBd(String aNomeCampo) {
        int vIndiceCampoRegistroBd = getIndiceCampoRegistroBd(aNomeCampo,this);
        if (vIndiceCampoRegistroBd == -1) {
            vIndiceCampoRegistroBd = getIndiceCampoRegistroBd(aNomeCampo,getCamposComplemento());
        }
        return vIndiceCampoRegistroBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CCampoRegistroBd getCampoRegistroBdComponente(Component aComponenteCampo, Vector aVetorCamposRegistroBd) {
        CCampoRegistroBd vCampoRegistroBd = null;
        CCampoRegistroBd vCampoRegistroBdLocalizado = null;
        int vNumCampos = aVetorCamposRegistroBd.size();
        CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = null;
        for (int vContador = 0; (vContador < vNumCampos) && (vCampoRegistroBdLocalizado == null); vContador++) {
            vCampoRegistroBd = (CCampoRegistroBd)aVetorCamposRegistroBd.get(vContador);
            if (vCampoRegistroBd != null) {
                vEstruturaCampoRegistroBd = vCampoRegistroBd.getEstruturaCampoRegistroBd();
                boolean vAchou = false;
                Component c = vEstruturaCampoRegistroBd.getComponente();
                if (c instanceof CCalendarioComboBox) {
                    if (aComponenteCampo instanceof JFormattedTextField) {
                        CCalendarioComboBox ccb = (CCalendarioComboBox)c;
                        vAchou = (ccb.getComponenteTextoFormatado() == aComponenteCampo);
                    }
                    else
                        if (aComponenteCampo instanceof CCalendarioComboBox) {
                            vAchou = (c == aComponenteCampo);
                        }
                        else
                            vAchou = false;
                }
                else {
                    vAchou = (c == aComponenteCampo);
                }
                if (vAchou) {
                    vCampoRegistroBdLocalizado = vCampoRegistroBd;
                }
            }
        }
        return vCampoRegistroBdLocalizado;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CCampoRegistroBd getCampoRegistroBdComponente(Component aComponenteCampo) {
        CCampoRegistroBd vCampoRegistroBd = getCampoRegistroBdComponente(aComponenteCampo,this);
        if (vCampoRegistroBd == null) {
            vCampoRegistroBd = getCampoRegistroBdComponente(aComponenteCampo,getCamposComplemento());
        }
        return vCampoRegistroBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void setValoresOriginaisCamposRegistroBd(boolean aRestaurarOriginal, Vector aVetorCamposRegistroBd) {
        CCampoRegistroBd vCampoRegistroBd = null;
        CCampoRegistroBd vCampoRegistroBdLocalizado = null;
        int vNumCampos = aVetorCamposRegistroBd.size();
        for (int vContador = 0; (vContador < vNumCampos) && (vCampoRegistroBdLocalizado == null); vContador++) {
            vCampoRegistroBd = (CCampoRegistroBd)aVetorCamposRegistroBd.get(vContador);
            if (vCampoRegistroBd != null)
                vCampoRegistroBd.setValorOriginalCampo(aRestaurarOriginal);
                vCampoRegistroBd.setCores();
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void setValoresOriginaisCamposRegistroBd(boolean aRestaurarOriginal) {
        setValoresOriginaisCamposRegistroBd(aRestaurarOriginal,this);
        setValoresOriginaisCamposRegistroBd(aRestaurarOriginal,getCamposComplemento());
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void setCoresCamposRegistroBd(Vector aVetorCamposRegistroBd) {
        CCampoRegistroBd vCampoRegistroBd = null;
        CCampoRegistroBd vCampoRegistroBdLocalizado = null;
        int vNumCampos = aVetorCamposRegistroBd.size();
        for (int vContador = 0; (vContador < vNumCampos) && (vCampoRegistroBdLocalizado == null); vContador++) {
            vCampoRegistroBd = (CCampoRegistroBd)aVetorCamposRegistroBd.get(vContador);
            if (vCampoRegistroBd != null)
                vCampoRegistroBd.setCores();
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void setCoresCamposRegistroBd() {
        setCoresCamposRegistroBd(this);
        setCoresCamposRegistroBd(getCamposComplemento());
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public int getIndiceCampoRegistroBdComponente(
        Component aComponenteCampo, Vector aVetorCamposRegistroBd) {
        //
        CCampoRegistroBd vCampoRegistroBd = null;
        CCampoRegistroBd vCampoRegistroBdLocalizado = null;
        int vIndiceCampoRegistroBdLocalizado = -1;
        int vNumCampos = aVetorCamposRegistroBd.size();
        CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = null;
        for (int vContador = 0; (vContador < vNumCampos) && (vCampoRegistroBdLocalizado == null); vContador++) {
            vCampoRegistroBd = (CCampoRegistroBd)aVetorCamposRegistroBd.get(vContador);
            if (vCampoRegistroBd != null) {
                vEstruturaCampoRegistroBd = vCampoRegistroBd.getEstruturaCampoRegistroBd();
                if (aComponenteCampo == vEstruturaCampoRegistroBd.getComponente()) {
                    vCampoRegistroBdLocalizado = vCampoRegistroBd;
                    vIndiceCampoRegistroBdLocalizado = vContador;
                }
            }
        }
        return vIndiceCampoRegistroBdLocalizado;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public int getIndiceCampoRegistroBdComponente(Component aComponenteCampo) {
        int vIndiceCampoRegistroBd = getIndiceCampoRegistroBdComponente(aComponenteCampo,this);
        if (vIndiceCampoRegistroBd == -1) {
            vIndiceCampoRegistroBd = getIndiceCampoRegistroBdComponente(aComponenteCampo,getCamposComplemento());
        }
        return vIndiceCampoRegistroBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void setCamposRegistroBd(ResultSet aRs, Vector aVetorCamposBd, boolean aCamposGradeBd) {
        CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = null;
        int vNumCamposRegistroBd = fEstruturaRegistroBd.getNumCamposRegistroBd();
        for (int vContador = 0; vContador <vNumCamposRegistroBd; vContador++) {
            vEstruturaCampoRegistroBd = fEstruturaRegistroBd.getEstruturaCampoRegistroBd(vContador);
            boolean vCampoGradeBd = vEstruturaCampoRegistroBd.getCampoGradeBd();
            if ((!aCamposGradeBd && !vCampoGradeBd) || (aCamposGradeBd && vCampoGradeBd)) {
                CCampoRegistroBd vCampoRegistroBd = new CCampoRegistroBd(this,vEstruturaCampoRegistroBd,aRs);
                aVetorCamposBd.add(vCampoRegistroBd);
            }
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void setCamposCabecalhoRegistroBd(ResultSet aRsCabecalhoBd) {
        setCamposRegistroBd(aRsCabecalhoBd,this,true);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void setCamposComplementoRegistroBd(ResultSet aRsComplementoRegistroBd) {
        setCamposRegistroBd(aRsComplementoRegistroBd,getCamposComplemento(),false);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CCampoRegistroBd atualizarCampoRegistroBd(
        Component aComponente, CCampoRegistroBd aCampoRegistroBd) {
        //
        if ((aCampoRegistroBd != null) && (aCampoRegistroBd.getIgualAnterior())) {
            return aCampoRegistroBd;
        }
        //
        Component vComponente = null;
        CCampoRegistroBd vCampoRegistroBd = null;
        CEstruturaCampoRegistroBd e = null;
        if (aComponente == null) {
            if (aCampoRegistroBd == null) {
                return null;
            }
            else
            {
                vCampoRegistroBd = aCampoRegistroBd;
                e = vCampoRegistroBd.getEstruturaCampoRegistroBd();
                vComponente = vCampoRegistroBd.getEstruturaCampoRegistroBd().getComponente();
            }
        }
        else {
            vComponente = aComponente;
            if (vComponente instanceof JFormattedTextField) {
                try {
                    ((JFormattedTextField)vComponente).commitEdit();
                } catch (ParseException ex) {
                    Logger.getLogger(CRegistroBd.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            vCampoRegistroBd = getCampoRegistroBdComponente(vComponente);
            e = vCampoRegistroBd.getEstruturaCampoRegistroBd();
            if (e instanceof CEstruturaCampoRegistroBdLookup) {
                CEstruturaCampoRegistroBdLookup el = (CEstruturaCampoRegistroBdLookup)e;
                CCampoRegistroBd c = el.getCampoBdChavePrimaria();
                vCampoRegistroBd.setValorCampoBd(c.getValorCampoBdStr());
//              vCampoRegistroBd.setValorCampoBd(el.getValorChavePrimariaStr());
            }
            else {
                //vCampoRegistroBd.setValorCampoBd(e.getValorComponenteStr());
                Object o = vCampoRegistroBd.getValorComponente();
                vCampoRegistroBd.setValorCampoBd(o);
            }
            e.setCores(true,vCampoRegistroBd.getIgualOriginal());
        }
        return vCampoRegistroBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void AtualizarComponentesCamposRegistroBd(Vector aVetorCamposRegistroBd) {
        int vNumCampos = aVetorCamposRegistroBd.size();
        for (int vContador = 0; vContador < vNumCampos; vContador++) {
            CCampoRegistroBd c = (CCampoRegistroBd)aVetorCamposRegistroBd.get(vContador);
            CEstruturaCampoRegistroBd e = c.getEstruturaCampoRegistroBd();
            c.setValorComponente(c.getValorCampoBd());
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CCampoRegistroBd getCampoComplemento(int aIndice) {
        //carregarCamposComplementoRegistroBd();
        if ((aIndice < 0) || (aIndice >= fCamposComplementoRegistroBd.size()))
            return null;
        else
            return (CCampoRegistroBd)fCamposComplementoRegistroBd.get(aIndice);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void eventoAntesAtualizarPainelBd() {
        fEstruturaRegistroBd.eventoAntesAtualizarPainelBd(this);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void eventoDepoisAtualizarPainelBd() {
        fEstruturaRegistroBd.eventoDepoisAtualizarPainelBd(this);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void atualizarPainelBd(boolean aFocar) {
        eventoAntesAtualizarPainelBd();
        AtualizarComponentesCamposRegistroBd(this);
        AtualizarComponentesCamposRegistroBd(getCamposComplemento());
        eventoDepoisAtualizarPainelBd();
        if (aFocar) focarPrimeiroComponente();
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public boolean focarPrimeiroComponente() {
        return fEstruturaRegistroBd.focarPrimeiroComponente();
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CCampoRegistroBd getCampoRegistroBdChavePrimaria() {
        return getCampoRegistroBd(fEstruturaRegistroBd.getNomeChavePrimaria());
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void setValorCampoRegistroBdChavePrimaria(Integer aValorChavePrimaria) {
        CCampoRegistroBd c = getCampoRegistroBd(fEstruturaRegistroBd.getNomeChavePrimaria());
        if (c != null) {
            if (aValorChavePrimaria == null)
                c.setValorCampoBd(aValorChavePrimaria);
            else
                c.setValorCampoBd(aValorChavePrimaria.toString());
        }
        //aValorChavePrimaria
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void setValorCampoRegistroBdDescricao(String aValorDescricao) {
        CCampoRegistroBd c = getCampoRegistroBd(fEstruturaRegistroBd.getNomeCampoDescricao());
        if (c != null) {
            c.setValorCampoBd(aValorDescricao);
        }
        //aValorChavePrimaria
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void setValorCampo(String aNomeCampo, Object aValor, boolean aAtualizarComponente) {
        CCampoRegistroBd c = getCampoRegistroBd(aNomeCampo);
        if (c != null)
            c.setValorCampoBd(aValor,aAtualizarComponente);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public Object getValorCampoRegistroBd(String aNomeCampo) {
        CCampoRegistroBd c = getCampoRegistroBd(aNomeCampo);
        if (c != null)
            return c.getValorCampoBd();
        else
            return null;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public String getValorCampoRegistroBdStr(String aNomeCampo) {
        CCampoRegistroBd c = getCampoRegistroBd(aNomeCampo);
        if (c != null)
            return c.getValorCampoBdStr();
        else
            return null;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
}

